//
//  FoodCollcetListCell.m
//  caiPu
//
//  Created by mac1 on 2025/6/13.
//

#import "PUCollcetListCell.h"

@implementation PUCollcetListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initViews];
    }
    return  self;
}
-(void)initViews{

    CGFloat width=_window_width;
    self.bgView=[[UIView alloc]init];
//    self.bgView.frame=CGRectMake(12*KWidthScale, 12*KWidthScale, width-12*KWidthScale*2, 96*KWidthScale);
    self.bgView.frame=CGRectMake(0, 0, width, 96*KWidthScale);
    self.bgView.backgroundColor=[UIColor whiteColor];
//    self.bgView.layer.cornerRadius = 10;
    self.bgView.layer.masksToBounds=YES;
    [self.contentView addSubview:self.bgView];
    
    self.xianView=[[UIView alloc]init];
    self.xianView.frame=CGRectMake(0, 96*KWidthScale-1, width, 1);
    self.xianView.backgroundColor=[UIColor colorWithHex:0xF4F4F4];
    [self.bgView addSubview:self.xianView];
    
    
    self.img=[[UIImageView alloc]init];
    self.img.contentMode=UIViewContentModeScaleAspectFill;
    self.img.layer.masksToBounds=YES;
    self.img.backgroundColor=[UIColor colorWithHex:0xDCDCDC];
    [self.bgView addSubview:self.img];
    
    self.flowers_nameLab = [[UILabel alloc] init];
    self.flowers_nameLab.font=[UIFont boldSystemFontOfSize:18];
    self.flowers_nameLab.numberOfLines=0;
    self.flowers_nameLab.textColor = [UIColor blackColor];
    [self.bgView addSubview:self.flowers_nameLab];
    
    self.scLab = [[UILabel alloc] init];
    self.scLab.text=@"食材1、食材2、食材3、食材4、食材5、食材6、食材7";
    self.scLab.font=[UIFont systemFontOfSize:14];
    self.scLab.textColor = [UIColor grayColor];
    [self.bgView addSubview:self.scLab];

    
    self.zanBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [self.zanBtn setImage:[UIImage imageNamed:@"shoucang_No"] forState:UIControlStateNormal];
    [self.zanBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.zanBtn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
    self.zanBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.zanBtn setTitle:@" 100+人已收藏" forState:UIControlStateNormal];
    [self.bgView addSubview:self.zanBtn];
    
   
}

//获取当前的时间
-(NSString*)getCurrentTimes{
     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
     // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
     [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
     //现在时间,你可以输出来看下是什么格式
     NSDate *datenow = [NSDate date];
     //----------将nsdate按formatter格式转成nsstring
     NSString *currentTimeString = [formatter stringFromDate:datenow];
     NSLog(@"currentTimeString =  %@",currentTimeString);
     return currentTimeString;
}

//计算有行距的文本的文本高度
-(CGFloat)getSpaceLabelHeight:(NSString *)str withAttrDict:(NSMutableDictionary *)dict withWidth:(CGFloat)width {
    CGSize size = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
    return size.height;
}


-(void)setFoodModel:(PUFoodModel *)model type:(int)type{
    _foodModel=model;
    if(type==0){
        [self.zanBtn setImage:[UIImage imageNamed:@"shoucang_No"] forState:UIControlStateNormal];
        [self.zanBtn setTitle:[NSString stringWithFormat:@" %ld人已收藏",(long)model.count] forState:UIControlStateNormal];
    }else{
        [self.zanBtn setImage:[UIImage imageNamed:@"观看时间"] forState:UIControlStateNormal];
        [self.zanBtn setTitle:[NSString stringWithFormat:@" 观看时间：%@",model.time] forState:UIControlStateNormal];
    }

    [self.img sd_setImageWithURL:[NSURL URLWithString:model.cover]];
    self.scLab.text=@"";
    
    
    //设置行距，并根据文本计算高度
    ////// ////// ////// ////// ////// //////
    //富文本属性
    NSMutableDictionary *textDict = [NSMutableDictionary dictionary];
    //基本属性设置
    //字体颜色
    textDict[NSForegroundColorAttributeName] = [UIColor blackColor];
    //字号大小
    textDict[NSFontAttributeName] = [UIFont boldSystemFontOfSize:18];
    //段落样式
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    //行间距
    paraStyle.lineSpacing = 2.0;
    //首行文本缩进
//  paraStyle.firstLineHeadIndent = 20.0;
    //使用
    //文本段落样式
    textDict[NSParagraphStyleAttributeName] = paraStyle;
    //赋值
    self.flowers_nameLab.attributedText = [[NSAttributedString alloc] initWithString:model.name attributes:textDict];
   
    
    self.img.frame=CGRectMake(12*KWidthScale,12*KWidthScale, 72*KWidthScale,72*KWidthScale);
    self.img.layer.cornerRadius = 4;
    
    CGFloat flowers_nameLabw=self.bgView.frame.size.width-(self.img.frame.origin.x+self.img.frame.size.width+12*KWidthScale*2);
    CGFloat size = [self getSpaceLabelHeight:model.name withAttrDict:textDict withWidth:flowers_nameLabw];
    self.flowers_nameLab.frame=CGRectMake(self.img.frame.origin.x+self.img.frame.size.width+12*KWidthScale, 12*KWidthScale, flowers_nameLabw, size);
    
    self.scLab.frame=CGRectMake(self.img.frame.origin.x+self.img.frame.size.width+12*KWidthScale, 45*KWidthScale, flowers_nameLabw, 15);
    
    self.zanBtn.frame=CGRectMake(self.img.frame.origin.x+self.img.frame.size.width+12*KWidthScale, self.scLab.frame.origin.y+self.scLab.frame.size.height+8*KWidthScale, flowers_nameLabw, 18);

}
-(void)xjBtnClick{
    if ([self.delegate respondsToSelector:@selector(clickBtn:)]) {
        [self.delegate clickBtn:self];
    }
}


@end
